package model

import (
	"gorm.io/gorm"
	"time"
	"wechat_1004/global"
)

type Message struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:ID;primaryKey;not null;" json:"id"`  // ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`          // 用户ID
	FriendId  int32          `gorm:"column:friend_id;type:int;comment:联系人ID;not null;" json:"friend_id"`     // 联系人ID
	Content   string         `gorm:"column:content;type:varchar(300);comment:聊天内容;not null;" json:"content"` // 聊天内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (m *Message) TableName() string {
	return "message"
}

// 记录
func (m *Message) AddMessage() error {
	return global.DB.Create(&m).Error
}

// 查看
func (m *Message) FindMessage() (message []Message, err error) {
	err = global.DB.Where("user_id = ?", m.UserId).Where("friend_id = ?", m.FriendId).Find(&message).Error
	return
}

// 撤回
func (m *Message) DeleteMessage() error {
	return global.DB.Delete(&m).Error
}
